﻿using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.DependencyInjection;
using AVISOutBound.SSOServer.Authorization;
using AVISOutBound.SSOServer.Authorization.Members;
using AVISOutBound.SSOServer.Authorization.Roles;
using Microsoft.Extensions.DependencyInjection.Extensions;
using System;
using System.IdentityModel.Tokens.Jwt;
using Abp.Runtime.Security;
using IdentityModel;

namespace AVISOutBound.SSOServer.Identity
{
    public static class IdentityRegistrar
    {
        public static IdentityBuilder Register(IServiceCollection services)
        {
            services.AddLogging();

            var builder = services.AddIdentity<Member, Role>(
                    options =>
                    {
                        // Password settings
                        options.Password.RequireDigit = true;
                        options.Password.RequiredLength = 6;
                        options.Password.RequireNonAlphanumeric = false;
                        options.Password.RequireUppercase = false;
                        options.Password.RequireLowercase = false;
                        options.Password.RequiredUniqueChars = 6;

                        // Lockout settings
                        options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(30);
                        options.Lockout.MaxFailedAccessAttempts = 10;
                        options.Lockout.AllowedForNewUsers = true;

                        // User settings
                        options.User.RequireUniqueEmail = true;
                    })
                .AddUserManager<ABGUserManager>()
                .AddUserStore<ABGUserStore>()
                .AddRoleStore<ABGRoleStore>()
                .AddSignInManager<ABGSignInManager>()
                .AddDefaultTokenProviders();

            // 注入服务
            // lulus:已经在上方添加过了
            //services.TryAddScoped<IUserStore<Member>, ABGUserStore>();
            //services.TryAddScoped<IRoleStore<Role>, ABGRoleStore>();

            return builder;
        }
    }
}
